package com.ruoyi.activiti;
//import javafx.concurrent.Task;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * 5.5 任务执行
 */
public class ActivitiCompleteTask {
    public static void main(String[] args) {
//      complete();
    }

    @Autowired
    private SysUserServiceImpl userService;

    public static void complete(Task task, SysUser  nextUser)
    {
        System.out.println("...开始执行任务... task");
        //1.获取ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取taskService
        TaskService taskService = processEngine.getTaskService();
        //3.根据流程定义的key，负责人assignee来实现当前用户的任务列表查询
        try{
            //  taskService.com
            //找到张燕茹的任务，并 结束任务
            String taskId = task.getId();// ActivitiTaskQuery.getInstance().getTask().getId();
            //执行任务异常！失败原因e=Unknown property used in expression: #{inputUser}
            SysUser user = ShiroUtils.getSysUser();
            //传入下一个参数
            Map<String,Object> variables = new HashMap<String,Object>();
            variables.put("审批人", nextUser.getUserName());//"张三");
            variables.put("userId", nextUser.getUserId());//"张燕茹");
            variables.put("inputUser", nextUser.getUserId());//"张燕茹"); //不知道为啥第二个节点的assignee变量变成了这个
            //完成任务并向下传递参数，第一个参数taskId 是ru_task表的ID_,第二个参数是向下传递的参数
            taskService.complete(taskId, variables); //这个里面的id是 ru_task表里面的ID，执行成功，任务就不在这个运行任务表里面了，二次执行会报错
            System.out.println("...任务执行完毕...执行taskId："+taskId);
        }catch (Exception e)
        {
            System.out.println("执行任务异常！失败原因e="+e.getMessage());
        }
    }
}
